package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

public class StudentIm implements Student1{

    @Override
    public String addStudent()//添加学生
    {
        Student2  student=InputUtil.getInputObject(new Student2());
        System.out.println("添加学生");
        String sql="INSERT INTO student1(name,age,score) " +
                "VALUES ("+"'"+student.getName()+"'"+","+student.getAge()+","+student.getScore()+")";
        return sql;
    }
    @Override
    public String deleteStudent(String name)//删除学生
    {
        System.out.println("删除学生");
        String sql="DELETE FROM student1 WHERE name="+"'"+name+"'";
        return sql;
    }
    @Override
    public String  updateStudent(String name)//修改学生信息
    {
        System.out.println("修改学生信息");
        Student2 student=InputUtil.getInputObject(new Student2());
        String sql="UPDATE student1 SET name="+"'"+student.getName()+"'"+","+"age="+student.getAge()+","+"score="+student.getScore()+" WHERE name="+"'"+name+"'";
        return sql;
    }
    @Override
    public String queryAllStudent()//查询所有学生信息
    {
        System.out.println("查询所有学生信息");
        String sql="SELECT * FROM student1";
        return sql;
    }

    @Override
    public String queryStudentById(int id) //根据id查询学生信息
    {
        System.out.println("根据id查信息");
        String sql="SELECT * FROM student1 WHERE id="+id;
        return sql;
    }




    public static Connection getConnection() throws Exception
    {
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8";
        String username="root";
        String password="root";
        Connection conn= DriverManager.getConnection(url,username,password);
        return conn;
    }

}
